package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.hosp.globalexp.YyghException;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.service.feign.DictFeignClient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    DictFeignClient dictFeignClient;
    @Autowired
    UserInfoService userInfoService;
    /**
     * 查询当前用户的就诊人列表
     * (1)从请求头中获取,根据就诊人id查询到了就诊人列表
     */
    @Override
    public List<Patient> findPatientList(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        //将每个就诊人列表进行遍历
        patientList.forEach(patient -> {
            this.packPatient(patient);
        });

        return patientList;
    }

    /**
     * 根据就诊人id查询
     * @param id
     * @return
     */
    @Override
    public Patient findById(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }

    /**
     * 添加就诊人,及对添加就诊人的限制
     * 1.注册账号(一个用户)最多可以同时绑定四个就诊人
     * 2.注册账号每自然年内,最多可以绑定八位就诊人
     * 3.同一手机号,最多同时可以被八位就诊人作为联系电话
     * @param patient
     */
    @Override
    public void addPatient(Patient patient) {
        //1.注册账号(一个用户)最多可以同时绑定四个就诊人
        Long userId = patient.getUserId();
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Integer integer = baseMapper.selectCount(queryWrapper); //统计未被删除的数量
        if(integer>=4){
            throw new YyghException(20001,"注册账号(一个用户)最多可以同时绑定四个就诊人");
        }

        //2.该用户每年的就诊人数量最多8个(包括已被删除的)
        //select count(1) From Patient where year(create_Time)='2023' and user_id=?
        String yyyy = new DateTime().toString("yyyy");
        Integer yearAndUserId = baseMapper.getCountByYearAndUserId(yyyy, userId);
        if(yearAndUserId>=8){
            throw new YyghException(20001,"该用户每年的就诊人数量最多8个");
        }

        //3.同一手机号,最多同时可以被八位就诊人作为联系电话
        String contactsPhone = patient.getContactsPhone();
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("contacts_phone",contactsPhone);
        Integer n = baseMapper.selectCount(wrapper);
        if(n>=8){
            throw new YyghException(20001,"同一手机号,最多同时可以被八位就诊人作为联系电话");
        }
        baseMapper.insert(patient);
    }

    //为Patient对象的param中赋值certificatesTypeString
    //param.certificatesTypeString
    private void packPatient(Patient patient) {
        //就诊人的证件类型(dict表中的value,value具有唯一性,就调用一个参数的方法,不具有唯一性就调用两个参数的方法)
        String certificatesType = patient.getCertificatesType();
        String provinceCode = patient.getProvinceCode();
        String cityCode = patient.getCityCode();
        String districtCode = patient.getDistrictCode();

        String provinceString="";
        String cityString ="";
        String districtString ="";
        //当前用户服务调用数据字典服务
        //如果feign远程调用时,参数为空
        if(!StringUtils.isEmpty(certificatesType)){
            String name = dictFeignClient.getName(certificatesType);
            patient.getParam().put("certificatesTypeString",name);
        }
        if(!StringUtils.isEmpty(provinceCode)){
             provinceString = dictFeignClient.getName(provinceCode);
            patient.getParam().put("provinceString",provinceString);
        }

        if(!StringUtils.isEmpty(cityCode)){
            cityString = dictFeignClient.getName(cityCode);
            patient.getParam().put("cityString",cityString);
        }

        if(!StringUtils.isEmpty(districtCode)){
            districtString = dictFeignClient.getName(districtCode);
            patient.getParam().put("districtString",districtString);
        }
        patient.getParam().put("fullAddress",provinceString+cityString+districtString+patient.getAddress());
        patient.getParam().put("userPhone",userInfoService.getById(patient.getUserId()).getPhone());
    }
}
